/*****************************************************************************
Copyright 2006 Kervin L. Pierre
    EMAIL: otlkcon@adevsoft.com

This file is part of Otlkcon.

Otlkcon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Otlkcon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with Otlkcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*******************************************************************************/

#pragma once
#include "StdAfx.h"
#include "otlkcon_account.h"
#include "otlkcon_event.h"

#include <vector>
#include <map>

#ifdef OTLKCON_XP_CALDAV
#include <protocols/caldav/caldav.h>
#endif
#ifdef OTLKCON_XP_FILE
#include <protocols/file/file.h>
#endif

using namespace std;

class O_IXPProvider;
class O_IXPLogon;

class O_IXPLogonAccount: public otlkcon_account_details
{
public:
    O_IXPLogonAccount( );
    O_IXPLogonAccount( wstring idStr, int t, O_IXPLogon *xpp );
    virtual HRESULT Logon() = 0;
    virtual HRESULT Init() = 0;
    virtual HRESULT Sync() = 0;

    virtual HRESULT ProcessEvent( otlkcon_event_struct *ev ) = 0;

    O_IXPLogon *lpXPLogon;
    O_ENTRYID *entryID;
};

class O_IXPLogonAccountCalDAV : public O_IXPLogonAccount
{
public:
    O_IXPLogonAccountCalDAV( );
    O_IXPLogonAccountCalDAV( wstring idStr, int t, O_IXPLogon *xpp );
    CaldavConnection *caldavConn;

    HRESULT Logon();
    HRESULT Init();
    HRESULT Sync();

    HRESULT OnSyncAppointmentNameResult( otlkcon_event_struct *event );
    HRESULT ProcessEvent( otlkcon_event_struct *event );
    HRESULT OnUploadICal( otlkcon_event_struct *ev );
    HRESULT OnDelAppointments( otlkcon_event_struct *ev );
};

class O_IXPLogonAccountFile : public O_IXPLogonAccount
{
public:
    O_IXPLogonAccountFile( );
    O_IXPLogonAccountFile( wstring idStr, int t, O_IXPLogon *xpp );
    FileConnection *fileConn;

    HRESULT Logon();
    HRESULT Init();
    HRESULT Sync();

    HRESULT OnSyncAppointmentNameResult( otlkcon_event_struct *event );
    HRESULT ProcessEvent( otlkcon_event_struct *event );
};

class O_IXPLogon : public IXPLogon
{
public:
	MAPI_IUNKNOWN_METHODS(IMPL);
    MAPI_IXPLOGON_METHODS(IMPL);

    O_IXPLogon(void);
    ~O_IXPLogon(void);

    HRESULT Create(O_IXPProvider *xpp, otlkcon_memory *m);
    HRESULT Destroy();

    HINSTANCE hInstance;
    LPMAPISUP lpMAPISup;

    ULONG   objectId;

    O_IXPProvider *lpXPP;

    // Initiate the synchronization process
    HRESULT SyncAccount( wstring idStr );
    HRESULT SyncAccounts();

    // List of registered accounts
    map<wstring,O_IXPLogonAccount*> accounts;
    otlkcon_memory *memObj;

private:
    ULONG cRef;
	CRITICAL_SECTION cs;
    

    ULONG statusCode;
    STDMETHODIMP SetStatusCode();

};

